package sys;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import javax.swing.event.TableModelEvent;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author Jonas
 */
public class BrowseSourceTableModel extends AbstractTableModel implements DownloadProgressListener {

    private static List<Video> videoList;
    private DecimalFormat nf;

    public BrowseSourceTableModel() {
        super();
        videoList = new ArrayList<Video>();
        this.nf = new DecimalFormat("000");
    }

    public int getRowCount() {
        return videoList.size();
    }

    public int getColumnCount() {
        return 4;
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
        switch(columnIndex) {
            case 0: return videoList.get(rowIndex).getGame();
            case 1: return videoList.get(rowIndex).getProducer();
            case 2: return videoList.get(rowIndex).getSource();
            case 3: return videoList.get(rowIndex).getTitle();
            //case 4: return videoList.get(rowIndex).getDownloadedTo();
            default: return "Missing";
        }
    }
    
    public void replaceVideosWith(List<Video> videos) {
    	videoList.clear();
    	videoList.addAll(videos);
    	this.fireTableDataChanged();
    }

    public void addVideo(Video video) {
        if(!videoList.contains(video)) {
            videoList.add(video);
            this.fireTableDataChanged();
        }
    }
    
    public void removeVideo(Video video) {
        if(videoList.contains(video)) {
            videoList.remove(video);
            this.fireTableDataChanged();
        }
    }

    public void removeVideoAtIndexes(int[] indexes) {
        int affected = 0;
        if(videoList.size() >= indexes.length) {
            for(int i : indexes) {
                videoList.remove(i-affected);
                affected++;
            }
        }
        this.fireTableDataChanged();
    }

    public Video getVideo(Video video) {
        return videoList.get(videoList.indexOf(video));
    }

    public Video getVideo(int index) {
        return videoList.get(index);
    }

    public List<Video> getVideos() {
        return videoList;
    }

    public synchronized void updateDownloadProgress(Video video) {
        if(videoList.indexOf(video) != -1) {
            this.fireTableChanged(new TableModelEvent(this, videoList.indexOf(video)));
        }
    }

}
